import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * 亿赛通加密
 */
public class EncryptEsafenet {
    public static void main(String[] args) throws IOException {

        String path = "d:\\AA.pdf";
        System.out.println("---------判断加密文件,未加密--------------");
        boolean encrypted = isEncrypted(path);
        System.out.println(path + (encrypted?"未加密":"已加密"));

        System.out.println("---------判断加密文件,已加密--------------");
        path = "d:\\BB.pdf";
        encrypted = isEncrypted(path);
        System.out.println(path + (encrypted?"未加密":"已加密"));

        System.out.println("---------文件加密--------------");
        path = "d:\\AA.pdf";
        String encryptPath = encrypt(path);
        System.out.println("文件已加密，存放于：" + encryptPath);

        System.out.println("---------文件解密--------------");
        path = "d:\\enc-AA.pdf";
        Path decryptPath = decrypt(path);
        System.out.println("文件已解密，存放于：" + decryptPath);
    }

    public static boolean isEncrypted(String path) throws IOException {
        File file = new File(path);

        String uri = "http://172.31.3.177:8081/sec-server/s/rs/uni/";
        CloseableHttpClient httpclient = HttpClients.createDefault();
        //String uri = "http://192.168.128.99:8081/sec-server/s/rs/uni/";//普通请求
        //中间件的默认端口是8081
//        String uri = "http://中间件ip地址:端口/sec-server/s/rs/uni/
        HttpPost post = new HttpPost(uri);
        //post.addHeader("method~name", "fileDecryptionRest"); //文件解密
        post.addHeader("method~name", "checkFileIsEncryptionRest"); //文件加密
//        post.addHeader("data~sysip", "0");
//        post.addHeader("data~sysflag", file.length() + "");
//        post.addHeader("data~secLevel", "10");
        System.out.println(file.length());
        try {
            InputStream in = new FileInputStream(file);

            AbstractHttpEntity entity = new InputStreamEntity(in);
            post.setEntity(entity);

            CloseableHttpResponse response = httpclient.execute(post);
            String result = response.getFirstHeader("data~returnFlag").getValue();
            if ("0".equals(result)) {//0 表示成功
                return true;
//                OutputStream out = Files.newOutputStream(new File(file.getParentFile(), "enc-" + file.getName()).toPath());
//
//                InputStream ins = response.getEntity().getContent();
//                byte[] bs = new byte[1024];
//                int len = 0;
//                while ((len = ins.read(bs)) > -1) {
//                    out.write(bs, 0, len);
//                }
//                out.flush();
//                out.close();
            }

            Header firstHeader = response.getFirstHeader("data~returnTip");
            if(firstHeader != null){
                String value = firstHeader.getValue();
                System.out.println(value);
            }
            return false;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static String encrypt(String path) throws IOException {
        File file = new File(path);

        String uri = "http://172.31.3.177:8081/sec-server/s/rs/uni/";
        CloseableHttpClient httpclient = HttpClients.createDefault();
        //String uri = "http://192.168.128.99:8081/sec-server/s/rs/uni/";//普通请求
        //中间件的默认端口是8081
//        String uri = "http://中间件ip地址:端口/sec-server/s/rs/uni/
        HttpPost post = new HttpPost(uri);
        //post.addHeader("method~name", "fileDecryptionRest"); //文件解密
        post.addHeader("method~name", "fileEncryptionRest"); //文件加密
        post.addHeader("data~fileOffset", "0");
        post.addHeader("data~counSize", file.length() + "");
        post.addHeader("data~secLevel", "10");
        System.out.println(file.length());
        try {
            InputStream in = new FileInputStream(file);

            AbstractHttpEntity entity = new InputStreamEntity(in);
            post.setEntity(entity);

            CloseableHttpResponse response = httpclient.execute(post);
            String result = response.getFirstHeader("data~returnFlag").getValue();
            if ("0".equals(result)) {//0 表示成功

                Path path1 = new File(file.getParentFile(), "enc-" + file.getName()).toPath();
                OutputStream out = Files.newOutputStream(path1);

                InputStream ins = response.getEntity().getContent();
                byte[] bs = new byte[1024];
                int len = 0;
                while ((len = ins.read(bs)) > -1) {
                    out.write(bs, 0, len);
                }
                out.flush();
                out.close();
                return path1.toString();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return uri;
    }

    public static Path decrypt(String path) throws IOException {
        File file = new File(path);

        String uri = "http://172.31.3.177:8081/sec-server/s/rs/uni/";
        CloseableHttpClient httpclient = HttpClients.createDefault();
        //String uri = "http://192.168.128.99:8081/sec-server/s/rs/uni/";//普通请求
        //中间件的默认端口是8081
//        String uri = "http://中间件ip地址:端口/sec-server/s/rs/uni/
        HttpPost post = new HttpPost(uri);
        //post.addHeader("method~name", "fileDecryptionRest"); //文件解密
        post.addHeader("method~name", "fileDecryptionRest"); //文件加密
        post.addHeader("data~fileOffset", "0");
        post.addHeader("data~counSize", file.length() + "");
//        post.addHeader("data~secLevel", "10");
        System.out.println(file.length());
        try {
            InputStream in = new FileInputStream(file);

            AbstractHttpEntity entity = new InputStreamEntity(in);
            post.setEntity(entity);

            CloseableHttpResponse response = httpclient.execute(post);
            String result = response.getFirstHeader("data~returnFlag").getValue();
            if ("0".equals(result)) {//0 表示成功

                Path path1 = new File(file.getParentFile(), "dec-" + file.getName()).toPath();
                OutputStream out = Files.newOutputStream(path1);

                InputStream ins = response.getEntity().getContent();
                byte[] bs = new byte[1024];
                int len = 0;
                while ((len = ins.read(bs)) > -1) {
                    out.write(bs, 0, len);
                }
                out.flush();
                out.close();
                return path1;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
